/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.util;


import org.proteusframework.api.IPlatformDelegate;

import java.util.List;

/**
 * Convenience class for asserting various states.
 *
 * @author Tacoma Four
 */
public final class Assert
{
    private Assert()
    {
    }


    /**
     * Asserts that the given object is not <code>null</code>. If this
     * is not the case, some kind of unchecked exception is thrown.
     *
     * @param object the value to test
     */
    public static void isNotNull(Object object)
    {
        if (object == null)
            throw new AssertionFailedException("Assert.isNotNull(object) failed");
    }

    /**
     * Asserts that the given object is not <code>null</code>. If this
     * is not the case, some kind of unchecked exception is thrown.
     * The given message is included in that exception, to aid debugging.
     *
     * @param object  the value to test
     * @param message the message to include in the exception
     */
    public static void parameterNotNull(Object object, String message)
    {
        if (object == null)
            throw new IllegalArgumentException("null argument:" + message);
    }

    /**
     * Asserts that the array is of a specific size.
     *
     * @param array   Array to inspect
     * @param length  Expected size
     * @param message the message to include in the exception
     */
    public static void assertSize(byte[] array, int length, String message)
    {
        if (array.length != length)
            throw new AssertionFailedException(message);
    }

    /**
     * Asserts that the list meets a minimum size requirement.
     *
     * @param list    List to evaluate
     * @param minSize Expected minimal size of the list
     * @param message the message to include in the exception
     */
    public static void isMinSize(List<?> list, int minSize, String message)
    {
        if (list.size() < minSize)
            throw new AssertionFailedException(message);
    }

    /**
     * Asserts that the list contains at least the specified minimum number of entries.
     *
     * @param list    List to evaluate
     * @param minSize Minimum number of entries that must be present in the list
     * @param message the message to include in the exception
     */
    public static void isAtLeasSize(List<?> list, int minSize, String message)
    {
        if (minSize > list.size())
            throw new AssertionFailedException(message);
    }

    /**
     * Asserts that the object has not been initialized.
     *
     * @param object  Object that must be null in order for the assertion to pass
     * @param message the message to include in the exception
     */
    public static void unitialized(Object object, String message)
    {
        if (null != object)
            throw new AssertionFailedException(message);
    }
}
